﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.IO;

using Microsoft.VisualBasic;

using System.Net;
using System.Net.Sockets;
using System.Threading;
using ServerApplication;
using Sim;
using Lists;

namespace ServerApplication
{
    static public class Program
    {
        static public ServerApplications sa = new ServerApplications();
        static public MyThreadPool mtp = new MyThreadPool();

        static void Main(string[] args)
        {
            
            Console.WriteLine("=======Server=======");
            DDList.DeserializeFromXML();
            TournamentList.DeserializeFromXML();
            UserList.DeserializeFromXML();
            TournamentRegisterList.DeserializeFromXML();
            Thread ta = new Thread(sa.Receive);
            Thread bc = new Thread(sa.Broadcast);
            Thread rbc = new Thread(sa.refreshBroadcast);
            bc.Start();
            ta.Start();
            rbc.Start();
            Thread.Sleep(10);
            for (int i = 0; i < IPList.ipList.Count; i++)
            {
                Console.WriteLine(IPList.ipList[i].ip);
            }
            try
            {
                for (int i = 0; i < TournamentList.tournamentList.Count; i++)
                {
                    if (TournamentList.tournamentList[i].isStarted)
                    {
                        Program.mtp.LaunchThread(TournamentList.tournamentList[i].title);
                        Console.WriteLine("Thread start was successful");
                    }
                        
                }
            }
            catch
            {
                Console.WriteLine("Error");
                for (int i = 0; i < TournamentList.tournamentList.Count; i++)
                {
                    if (TournamentList.tournamentList[i].isStarted)
                    {
                        Program.mtp.LaunchThread(TournamentList.tournamentList[i].title);
                        Console.WriteLine("Thread started through catch");
                    }
                }
            }
        }
    }
}
